package com.cyan.teamquestionnaire.controller;

import com.cyan.teamquestionnaire.beans.HttpResponseEntity;
import com.cyan.teamquestionnaire.entity.SecurityQuestion;
import com.cyan.teamquestionnaire.service.SecurityQuestionService;
import com.cyan.teamquestionnaire.utils.Constants;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Map;

@RestController
public class SecurityQuestionController {

    @Resource
    SecurityQuestionService securityQuestionService;

    @Resource
    HttpResponseEntity httpResponseEntity;

    // 新建密保问题, sq--SecurityQuestion
    @RequestMapping(value = "/sqCreate", method = RequestMethod.POST)
    public HttpResponseEntity sqlCreate(@RequestBody  SecurityQuestion securityQuestion) {

        int i = securityQuestionService.createSecurityQuestion(securityQuestion);
        if(i == 0) { // 已经设置过密保问题了，不能再新建，只能修改
            httpResponseEntity.setCode(Constants.EXIST_CODE);
            httpResponseEntity.setMessage(Constants.SECURITY_EXITS);
        } else {
            httpResponseEntity.setCode(Constants.SUCCESS_CODE);
            httpResponseEntity.setMessage(Constants.CREATE_MESSAGE);
        }
        return httpResponseEntity;
    }

    // 修改密保问题
    @RequestMapping(value = "/sqModify", method = RequestMethod.POST)
    public HttpResponseEntity sqModify(@RequestBody SecurityQuestion securityQuestion) {
        securityQuestionService.modifySecurityQuestion(securityQuestion);
        httpResponseEntity.setMessage(Constants.SECURITY_UPDATE);
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        // 传入修改后的问题
        httpResponseEntity.setData(securityQuestionService.getQuestionByName(securityQuestion.getUserName()));
        return httpResponseEntity;
    }

    // 查询密保问题（可能显示这个密保问题需要用到这个方法）
    @RequestMapping(value = "/sqSelect", method = RequestMethod.POST)
    public HttpResponseEntity sqSelect(@RequestBody Map<String,Object> map) {
        // 直接通过用户名查询密保即可
        String userName = (String) map.get("name");
        SecurityQuestion securityQuestion = securityQuestionService.getQuestionByName(userName);
        if(securityQuestion == null) {
            httpResponseEntity.setCode(Constants.EXIST_CODE);
            httpResponseEntity.setMessage(Constants.SECURITY_NO_EXITS);
        } else {
            httpResponseEntity.setCode(Constants.SUCCESS_CODE);
            httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
            httpResponseEntity.setData(securityQuestion);
        }
        return httpResponseEntity;
    }
}
